#!/usr/bin/python
# -*- coding: UTF-8 -*-
from config import *
import pandas as pd
import pymongo;
client=pymongo.MongoClient(MONGO_URL);
db=client["lagou"];
import re
import matplotlib.pyplot as plot
import numpy
from utils.GwordClude import *
import pyecharts as pc
# https://www.jianshu.com/p/b718c307a61c
import jieba
from io import StringIO
import seaborn as sns
import numpy as np
#指定默认字体
plot.style.use('ggplot')
plot.rcParams['font.sans-serif'] = ['SimHei']
plot.rcParams['font.family']='sans-serif'
def get_data():
    queryArgs = {}
    projectionFields = {'_id': False}  # 用字典指定
    result = db["jobs_data"].find(queryArgs,projectionFields)
    return result


def main():
    data = pd.read_csv("job_datas.csv", encoding="utf-8")
    print(len(data))
    data["city"] = data["city"].apply(fileter_city)
    data["salary"] = data["salary"].apply(filter_salary)
    data["exprience"] = data["job_requrie"].apply(get_experience)
    data["education"] = data["job_requrie"].apply(get_education)
    data["company_m"]=data["indestry_company"].apply(get_company_money)
    data["indusry"]=data["indestry_company"].apply(get_indusry)
    mean=data["salary"].mean()
    print(mean)
    data=data[data["city"]=="深圳"]
    data=data["硕士" != data["exprience"]]
    data=data[data["salary"]>18]
    data=data.drop_duplicates(subset='company_name')
    print(data["company_name"])

    print(data)
    # data=data.groupby("company_m").size().sort_values()
    # bar = pc.Bar('公司融资期数量分布')
    # bar.add('', data.index, data.values, mark_point=["max", "min", "average"], xaxis_rotate=45)
    # bar.render()
    # print(data.head())

def get_company_money(str):
    result=str.split("/")
    if result:
        result=result[1]
    else:
        result="未知"
    return result


def get_indusry(str):
    result=str.split("/")
    if result:
        result=result[0]
        if result:
            result=result.split(",")
            if result:
                result=result[0]
    else:
        result= "未知"
    return result.strip()




def education_salar(data):
    data["exprience"] = data["job_requrie"].apply(get_experience)
    data["education"] = data["job_requrie"].apply(get_education)
    data = data.groupby(by="education")["salary"].mean().sort_values()
    data = pd.DataFrame(data)
    print(data.head())
    sns.boxplot(x=data.index, y=data["salary"])
    plot.title("学历薪资分布")
    plot.savefig("salary_education.jpg")
    plot.show()


# 经验1年以下 / 大专
def get_experience(str):
    result=str.split("/")
    if result:
        result=result[0][2:]
    else:
        result= "未知"
    return result.strip()
def get_education(str):
    result = str.split("/")
    if result:
        result=result[1]
    else:
        result="未知"
    return result.strip()


def salary_distrubte(data):
    salary = data.groupby(by="city").mean()
    salary = salary.sort_values(by="salary", ascending=False)
    salary = salary.iloc[1:]
    salary = salary.head(10)
    salary = salary.applymap(lambda x: '%.2f' % x)
    top10_city_box = data.loc[data['city'].isin(salary.index), :]
    sns.violinplot(x='salary', y='city', data=top10_city_box)
    plot.title("薪资在城市分布")
    plot.savefig("salary_city_fenbu.jpg")
    plot.show()


def salary_show(data):
    data["city"] = data["city"].apply(fileter_city)
    data["salary"] = data["salary"].apply(filter_salary)
    data = data.groupby(by="city").mean()
    salary = data.sort_values(by="salary", ascending=False)
    salary = salary.iloc[1:]
    salary = salary.head(10)
    salary = salary.applymap(lambda x: '%.2f' % x)
    print(salary)
    bar = pc.Bar("各大城市平均薪资")
    bar.add("", salary.index, salary["salary"], mark_point=["max"], is_convert=True)
    bar.render()
    # print(data.describe())


def city_show(data):
    data["city"] = data["city"].apply(fileter_city)
    city = data.groupby(by="city").size()
    city = city.sort_values(ascending=False)
    print(city)
    bar = pc.Bar("城市职位数量")
    bar.add("", city.index, city.values, mark_point=["max"])
    bar.show_config()
    bar.render()


def fileter_city(str):
    pattern=re.compile(r"\[(.*?)·.*?\]")
    result=re.match(pattern,str)
    if result:
        return result.group(1)
    else:
        return str




def get_cloud_word(data):
    all_words = get_job_requets(data)
    generate_cloud_word(jieba.cut(all_words), "../utils/aaaa.jpg", "job_reuest.jpg")


def get_job_requets(data):
    result=list(data["job_des"])
    string=StringIO()
    for word in result:
        string.write(str(word))
    result=re.sub("\d","",string.getvalue())
    return result

def salary_alnary(data):
    print(data["salary"].mean())
    plot.title("拉钩网数据分析薪资分布")
    plot.ylabel("薪资数量")
    plot.xlabel("薪资数量单位 k")
    plot.hist(data["salary"])
    plot.savefig("lagou.jpg")
    plot.show()


def filter_salary(salary):
    patter=re.compile(r"(\d+).*?(\d+)\w")
    result=re.match(patter,salary)
    return (int(result.group(1))+int(result.group(2)))/2




if __name__ == '__main__':
    main()
    # get_experience("经验1年以下 / 大专")
    # print(get_indusry("移动互联网,硬件 / D轮及以上"))


